//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    people: null,
    spinShow: false,
    qRCodeMsg: '',
    teacher_classList: [],
    student_classList: [],
    student_add_class_code: null,
    teacher_create_class_name: null,
    card: [{
      img: {
        url: "/static/images/news/avatar_1.jpg"
      },
      title: {
        text: "老师"
      },
      tag: {
        text: "1小时前"
      },
      header: {
        bgcolor: "white",
        // line: true
      }
    }]
  },

  onShow: function () {
    //改成callback
    setTimeout(() => {
      this.setData({
        spinShow: true
      });
      this.indexLoad()
    }, 1000)

  },

  onPullDownRefresh() {
    this.setData({
      spinShow: true
    });
    if (!this.loading) {
      this.indexLoad()
    }
  },

  indexLoad: function () {
    let _this = this
    _this.setData({
      people: wx.getStorageSync("people")
    })

    if (wx.getStorageSync("myCode") != '' && wx.getStorageSync("myCode") != null && app.globalData.teacher_info['id'] != null && wx.getStorageSync("people") == 'teacher') {
      // console.log('teacher');
      app.formGet('/get_all_class_by_teacher_id?teacher_id=' + app.globalData.teacher_info['id']).then(res => {
        console.log('获取教师课程列表：', res);
        this.setData({
          teacher_classList: res,
          spinShow: false
        });
      }).catch(e => {
        _this.setData({
          spinShow: false
        })
      })
    }
    // console.log('app.globalData.student_info',app.globalData.student_info['id']);
    if (wx.getStorageSync("myCode") != '' && wx.getStorageSync("myCode") != null && app.globalData.student_info['id'] != null && wx.getStorageSync("people") == 'student') {
      // console.log('student');
      app.formPost('/get_all_class_by_student_id?', {
        'student_id': app.globalData.student_info['id']
      }).then(res => {
        console.log('获取学生课程列表：', res);
        this.setData({
          student_classList: res,
          spinShow: false
        });
      }).catch(e => {
        _this.setData({
          spinShow: false
        })
      })
    }

    wx.stopPullDownRefresh()
  },

  student_add_class: function () {
    let _this = this
    wx.showModal({
      title: '加入班级',
      editable: false, //显示输入框
      cancelText: '手动输入',
      confirmText: '扫码加入',
      success: res => {
        if (res.confirm) { //点击了确认
          wx.scanCode({
            success: (res) => {
              console.log("扫码结果:", res);
              _this.setData({
                student_add_class_code: res['result']
              })
              console.log('app.globalData.student_info', app.globalData.student_info)
              let data = {
                'invite_code': _this.data.student_add_class_code,
                'student_account': app.globalData.student_info['account'],
                'student_id': app.globalData.student_info['id'],
              }
              app.formPost('/add_course?', data).then(res => {
                // console.log(res)
                if (res['code'] == 0) {
                  console.log("加入班级失败")
                  app.message("加入班级失败", 'error')
                }
                if (res['code'] == 1) {
                  console.log("加入班级成功")
                  app.message("加入班级成功", 'success')
                }
                _this.indexLoad()
              })
            },
            fail: (res) => {
              console.log('扫码失败:', res);
            }
          })
        } else if (res.cancel) {
          wx.showModal({
            title: '加入班级',
            editable: true, //显示输入框
            placeholderText: '输入邀请码', //显示输入框提示信息
            success: res => {
              if (res.confirm) { //点击了确认
                // console.log(res.content)//用户输入的值
                _this.setData({
                  student_add_class_code: res.content
                })
                console.log('app.globalData.student_info', app.globalData.student_info)
                let data = {
                  'invite_code': _this.data.student_add_class_code,
                  'student_account': app.globalData.student_info['account'],
                  'student_id': app.globalData.student_info['id'],
                }
                app.formPost('/add_course?', data).then(res => {
                  // console.log(res)
                  if (res['code'] == 0) {
                    console.log("加入班级失败")
                    app.message("加入班级失败", 'error')
                  }
                  if (res['code'] == 1) {
                    console.log("加入班级成功")
                    app.message("加入班级成功", 'success')
                  }
                  _this.indexLoad()
                })
              } else {
                console.log('用户点击了取消')
                app.message("取消加入班级", 'error')
              }
            }
          })
        }
      }
    })

  },

  teacher_create_class: function () {
    let _this = this
    wx.showModal({
      title: '创建课程',
      editable: true, //显示输入框
      placeholderText: '输入课程名称', //显示输入框提示信息
      success: res => {
        if (res.confirm) { //点击了确认
          // console.log(res.content)//用户输入的值
          _this.setData({
            teacher_create_class_name: res.content
          })
          console.log('app.globalData.teacher_info', app.globalData.teacher_info)
          let data = {
            'course_name': res.content,
            'teacher_account': app.globalData.teacher_info['account'],
            'teacher_id': app.globalData.teacher_info['id']
          }
          app.formPost('/create_course?', data).then(res => {
            console.log('正在创建班级', res)
            _this.indexLoad()
          })
        } else {
          console.log('用户点击了取消')
        }
      }
    })
  },

  goto_classHomePage_index: function (e) {
    let data = e.currentTarget.dataset['list']
    console.log('点击的班级app.globalData.banji_info:', data);
    app.globalData.banji_info = data
    wx.navigateTo({
      url: '/pages/classHomePage/index/index',
    })
  },
})